<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-calendar"></i> 专业信息管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>专业编辑</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="form-box">
        <el-form
            ref="formRef"
            :rules="rules"
            :model="form.data"
            label-width="80px"
        >
          <el-form-item label="专业名称" prop="majorName">
            <el-input v-model="form.data.majorName"></el-input>
          </el-form-item>
          <el-form-item label="专业简称" prop="shortName">
            <el-input v-model="form.data.shortName"></el-input>
          </el-form-item>
          <el-form-item label="专业代码" prop="majorCode">
            <el-input v-model="form.data.majorCode" type="number" maxlength="1" show-word-limit></el-input>
          </el-form-item>
          <el-form-item label="专业介绍" prop="intro">
            <el-input v-model="form.data.intro"></el-input>
          </el-form-item>
          <el-form-item label="辅导员" prop="adviser">
            <el-input
                v-model="form.data.adviser"
                @focus="getAdviser()"
            ></el-input>
          </el-form-item>
          <!--          <el-form-item label="开设日期" prop="estabDate">-->
          <!--            <el-date-picker-->
          <!--              v-model="form.data.estabDate"-->
          <!--              type="date"-->
          <!--              placeholder="选择一个日期"-->
          <!--              format="YYYY-MM-DD"-->
          <!--              value-format="YYYY-MM-DD"-->
          <!--            >-->
          <!--            </el-date-picker>-->
          <!--          </el-form-item>-->
          <el-form-item label="所属学院" prop="collegeName">
            <el-select v-model="form.data.collegeId" placeholder="请选择学院">
              <el-option
                  v-for="item in collegeList"
                  :key="item.sid"
                  :label="item.collegeName"
                  :value="item.sid"
                  :disabled="item.collegeName!=='全学院'? false:true"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">提交</el-button>
            <el-button @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div>
      <el-dialog
          title="选择辅导员"
          v-model="dialogVisible"
          width="25.5%"
          :before-close="handleClose"
      >
        <el-table :data="adviserData" border style="width: 100%">
          <!-- <el-table-column prop="id" label="" width="120">
          </el-table-column> -->
          <el-table-column prop="userName" label="用户"/>
          <el-table-column prop="loginName" label="账号"/>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button @click="checkAdviser(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {reactive, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import request from "../../utils/request";
import {useRoute, useRouter} from "vue-router";
import {useStore} from "vuex";

export default {
  name: "AddMajor",
  setup() {
    const root = "/major/";
    const router = useRouter();
    const route = useRoute();
    const store = useStore();

    const collegeList = ref([]);
    request.get("/college").then((res) => {
      collegeList.value = res.data;
    });

    const rules = {
      majorName: [{required: true, message: "请输入专业名", trigger: "blur"}],
    };

    const formRef = ref(null);
    var form = reactive({
      data: {
        sid: "",
        collegeId: "",
        majorCode: "",
        majorName: "",
        shortName: "",
        adviser: "",
        adviserId: "",
        intro: "",
        estabDate: "",
      },
    });
    //编辑单个数据回显
    if (route.query.id) {
      request.get(root + route.query.id).then((res) => {
        form.data = res.data;
      });
    }
    // 提交
    const onSubmit = () => {
      // 表单校验
      formRef.value.validate((valid) => {
        if (valid) {
          var method = form.data.sid === "" ? "POST" : "PUT";
          request({
            url: root,
            method: method,
            data: form.data,
          }).then((res) => {
            if (res.code === "18") {
              ElMessage.error(res.msg);
            } else {
              ElMessage.success("提交成功！");
              // 关闭当前页面的标签页;
              store.commit("closeCurrentTag", {
                $router: router,
                $route: route,
              });
            }
            router.push("/admin/major");
          });
        } else {
          return false;
        }
      });
    };
    // 重置
    const onReset = () => {
      formRef.value.resetFields();
    };
    const dialogVisible = ref(false);
    const handleClose = (done) => {
      ElMessageBox.confirm("确定放弃选择或者关闭吗?")
          .then(() => {
            done();
          })
          .catch(() => {
            // catch error
          });
    };
    const adviserData = ref([]);
    const getAdviser = () => {
      dialogVisible.value = true;
      request.get("/user/queryUser").then((res) => {
        adviserData.value = res.data;
      });
    };
    const checkAdviser = (row) => {
      console.log(row);
      form.data.adviser = row.userName;
      form.data.adviserId = row.sid;
      if (form.data.adviserId != null) {
        dialogVisible.value = false;
      }
    };
    return {
      collegeList,
      rules,
      formRef,
      form,
      onSubmit,
      onReset,
      handleClose,
      dialogVisible,
      getAdviser,
      checkAdviser,
      adviserData,
    };
  },
};
</script>